package com.deyou.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.deyou.R;
import com.deyou.adapter.ChatFragmentAdapter;
import com.deyou.base.BaseActivity;
import com.deyou.fragment.ChatFragment;
import com.deyou.fragment.OrderFragment;
import com.deyou.model.HouseResourceDetailBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChatWithLandloadActivity extends BaseActivity {

    @BindView(R.id.iv_left_back)
    ImageView ivLeftBack;
    @BindView(R.id.tv_mid_title)
    TextView tvMidTitle;
    @BindView(R.id.ic_landload_order)
    ImageView icLandloadOrder;
    @BindView(R.id.ic_landload_chat)
    ImageView icLandloadChat;
    @BindView(R.id.landload_view_pager)
    ViewPager landloadViewPager;

    private String checkInDate;
    private String checkOutDate;
    private int stayPeople;
    private HouseResourceDetailBean detailBean;
    private OrderFragment orderFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_with_landload);
        ButterKnife.bind(this);
        initData();
        initView();
        initFragment();
    }

    private void initData(){
        checkInDate = getIntent().getExtras().getString("check_in_date");
        checkOutDate = getIntent().getExtras().getString("check_out_date");
        detailBean = (HouseResourceDetailBean) getIntent().getExtras().getSerializable("houseDetail");
        stayPeople = getIntent().getExtras().getInt("stayPeople");
    }

    private void initView(){
        tvMidTitle.setText("房东" + detailBean.resourceUserName);
    }

    private void initFragment() {
        orderFragment = OrderFragment.newInstance(checkInDate, checkOutDate, detailBean, stayPeople);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(orderFragment);
        fragments.add(new ChatFragment());
        ChatFragmentAdapter fragmentAdapter = new ChatFragmentAdapter(getSupportFragmentManager(), fragments);
        landloadViewPager.setAdapter(fragmentAdapter);

        landloadViewPager.setCurrentItem(1);
        landloadViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0){
                    icLandloadOrder.setBackgroundColor(getResources().getColor(R.color.color_005EAD));
                    icLandloadChat.setBackgroundColor(getResources().getColor(R.color.color_transparent));
                    icLandloadOrder.setImageResource(R.drawable.ic_landload_order_sel);
                    icLandloadChat.setImageResource(R.drawable.ic_landload_chat_nor);
                } else if (position == 1){
                    icLandloadChat.setBackgroundColor(getResources().getColor(R.color.color_005EAD));
                    icLandloadOrder.setBackgroundColor(getResources().getColor(R.color.color_transparent));
                    icLandloadOrder.setImageResource(R.drawable.ic_landload_order_nor);
                    icLandloadChat.setImageResource(R.drawable.ic_landload_chat_sel);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.iv_left_back, R.id.ic_landload_order, R.id.ic_landload_chat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left_back:
                finish();
                break;
            case R.id.ic_landload_order:
                landloadViewPager.setCurrentItem(0);
                break;
            case R.id.ic_landload_chat:
                landloadViewPager.setCurrentItem(1);
                break;
        }
    }
}
